<?php

class CheckoutController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->viewRenderer->setNoRender();
            $this->_helper->layout()->disableLayout();
        }
    }

    public function itemsnoAction(){
        $itemsNo = new Zend_Session_Namespace('itemsNo');
        echo $itemsNo->itemsNo;
    }

    public function addproductAction() {
        $userdata = new Zend_Session_Namespace('login');
        $itemsNo = new Zend_Session_Namespace('itemsNo');
        $itemsNo->itemsNo += 1;
        echo $itemsNo->itemsNo;
        if (isset($userdata->userObj)) {//user is login
            $product = new Application_Model_Product();
            $prod = $product->getProduct($this->getRequest()->getParam('prodid'));
            $myProducts = new Zend_Session_Namespace('products');
            $prod = $prod->toArray();

            if (!isset($myProducts->id)) {
                $myProducts->id = array();
            }
            array_push($myProducts->id, $prod[0]);
        } else {
            //redirect user to login page
            //',array('ajax'=>'yes') to check on this in AuthContoller.php to disaple layout for ajax
            $this->_helper->redirector('index', 'auth', null, array('ajax' => 'yes'));
        }
        //pre($prod[0]);
        //  $myProducts->id = array();
        //   pre($myProducts->id);
    }

    public function doneAction() {
        $myProducts = new Zend_Session_Namespace('products');
        $login = new Zend_Session_Namespace('login');
        //echo $login->username;
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $uid = $db->select()->from('users')->where("username=?", $login->username);
        $s = $db->fetchAll($uid);
        $user_id = $s[0]['id'];
        $request = $this->getRequest();
        $address = $request->getParam('address');
        //pre($request->getParams());

        $db->insert("order", array('status' => 'pending',
            'created_at' => date('Y-m-d'),
            'user_id' => $user_id,
            'address' => $address
        ));


        $order_id = $db->select()->from('order')->order("id desc")->limit(1);
        $order_id = $db->fetchAll($order_id);
        $order_id = $order_id[0]['id'];
        for ($i = 0; $i < count($myProducts->id); $i++) {
            //pre($myProducts->id[$i]);
            $quantity = $myProducts->id[$i]['quantity'];
            $id = $myProducts->id[$i]['id'];

            $qPurchased= $myProducts->data[$i + 3];
            $db->update("product", array('quantity' => ($quantity-$qPurchased)), "id = $id");

            $db->insert("orderline", array('product_id' => $myProducts->id[$i]['id'],
                'price' => $myProducts->id[$i]['price'],
                'quantity' => $qPurchased,
                'order_id' => $order_id
            ));
            $myProducts->id[$i]['quantity']=$myProducts->id[$i]['quantity']-$qPurchased;
            

            
        }
        unset ($myProducts->id);
        $myProducts = new Zend_Session_Namespace('itemsNo');
        unset($myProducts->itemsNo);
    }

    public function removeAction() {
        $myProducts = new Zend_Session_Namespace('products');
        $itemsNo = new Zend_Session_Namespace('itemsNo');
        $itemsNo->itemsNo -=1;
        for ($i = 0; $i < count($myProducts->id); $i++) {
            $request = $this->getRequest();
            if (array_key_exists($i, $myProducts->id)) {
                if ($myProducts->id[$i]['id'] == $request->getParam('id')) {
                    //    unset($myProducts->id[$i]);
                    unset($myProducts->id[$i]);
                    $myProducts->id = array_values($myProducts->id);
                    break;
                    //pre($myProducts->id);
                }
            }
        }
    }

    public function indexAction() {
        $myProducts = new Zend_Session_Namespace('products');

//                $myProducts->id = array(
//                    array('id' => '1', 'name' => 'flower1', 'discreption' => 'nice product one', 'img' => 'logo1.jpg', 'quantity' => '50'),
//                    array('id' => '2', 'name' => 'flower2', 'discreption' => 'nice product two', 'img' => 'logo2.jpg', 'quantity' => '59'),
//                    array('id' => '3', 'name' => 'flower3', 'discreption' => 'nice product three', 'img' => 'logo3.png', 'quantity' => '500'),
//                    array('id' => '4', 'name' => 'flower4', 'discreption' => 'nice product four', 'img' => 'logo4.jpg', 'quantity' => '100')
//                );
    }

    public function ordernowAction() {
        $myProducts = new Zend_Session_Namespace('products');
        $request = $this->getRequest();
        $myProducts->data = array_values($request->getParams());
    }

}

